SSVC.Wrap.Dojo.SetObject("SSVC.SVG.ActiveLabelElement",
 SSVC.Wrap.Dojo.Declare("ActiveLabelElement", SSVC.SVG.ActiveElement,
{
	TextArea: null,
	TextElementHelper: null,
	TextAreaHeight: null,
	currX: null,
	currY: null,

	_textRows: 1,
	constructor: function(fontSize)
	{
		this.FontSize = fontSize;
	},

	OnMouseUp: function(callElement)
	{
		if (callElement.ActionMode == SSVC.SVG.ActiveElement.Enums.ActionMode.Drawing)
		{
			callElement.TextArea.textContent = "";
		//	callElement.TextArea.style.width = +callElement.OX + "px";

			callElement.TextArea.focus();
		}
		callElement.inherited(arguments);
		//callElement.Node.zIndex = "5000000";
	},

	

	OnTextAreaKeyPress: function(callElement)
	{
		//callElement.InnerText.TextContent = callElement.TextArea.value;
		//var textAreaRect = callElement.TextArea.getBoundingClientRect();
		//var textAreaWidth = textAreaRect.right - textAreaRect.left;
		var newWidth = ((callElement.TextArea.value.length + 1) * 10);//callElement.TextArea.clientWidth; //

		if (callElement.OX <= newWidth)
			callElement.OX = newWidth;//callElement.TextArea.scrollWidth;
			//callElement.OX += callElement.Radius
			//callElement.Refresh();
			//callElement.OX += +callElement.Radius;


		//callElement._resizeTextHeight();


		if (arguments[1].keyCode == 13 && !arguments[1].ctrlKey && !arguments[1].shiftKey)
		{
			callElement.TextArea.blur();
			callElement.SelectMode = SSVC.SVG.ActiveElement.Enums.SelectMode.None;
			callElement.Refresh();
		}
	},

	OnTextAreaKeyUp: function(callElement)
	{
		var newWidth = callElement.OX - callElement.Radius;
		if (arguments[1].keyCode == 8 && callElement.TextArea.value != "" && newWidth >= callElement.OY)
		{
			callElement.OX -= +callElement.Radius;
		}
		//callElement.InnerText.TextContent = callElement.TextArea.value;
		//if (callElement.InnerText.Node.getBBox().width >= callElement.OY)
		//	callElement.OX = callElement.InnerText.Node.getBBox().width;
		//var textAreaRect = callElement.TextArea.getBoundingClientRect();
		//var textAreaWidth = textAreaRect.right - textAreaRect.left;

		callElement.startX = callElement.X + callElement.OX / 2;
		callElement.Refresh();
	},

	OnTextAreaFocus: function(callElement)
	{
		//callElement._setTextAreaTop();
		//callElement.Refresh();
	},

	OnTextAreaBlur: function(callElement)
	{
		//callElement.TextArea.readOnly = true;
	},

	OnTextAreaMouseDblClick: function(callElement)
	{
		//callElement.TextArea.readOnly = false;
		//callElement.TextArea.focus();
		
	},

	OnTextAreaMouseDown: function(callElement)
	{
		//callElement.TextArea.blur();
	},

	OnTopMouseDown: function()
	{ },

	Draw: function(startX, startY, currX, currY, shiftKey)
	{
		if (this.ActionMode != SSVC.SVG.ActiveElement.Enums.ActionMode.Drawing)
		{
			this.SelectMode = SSVC.SVG.ActiveElement.Enums.SelectMode.Contoured;
			this.ActionMode = SSVC.SVG.ActiveElement.Enums.ActionMode.Drawing;
		}
		if (this.ActionMode == SSVC.SVG.ActiveElement.Enums.ActionMode.Drawing)
		{
			this.startX = startX;
			this.startY = startY;
			this.Radius = this.FontSize;
			this.OY = this.OX = +this.FontSize *2;
			this.X = +startX - this.OX / 2;
			this.Y = +startY - this.OY - 20;
			//this.InnerText.X = this.X;
			//this.InnerText.Y = +this.Y + +this.FontSize;
			//this.InnerText.FontSize = this.FontSize * 1.2;
			//this.InnerText.Node.style.marginLeft = this.Radius + "px";
			//this.InnerText.Node.style.marginTop = "2px";
			this.TextArea.style.minWidth = this.OX + 'px';
			this.TextArea.style.minHeight = this.OY + 'px';
			this.Refresh(true);
		}
	},

	_resizeTextHeight: function()
	{
		//if (this.TextArea.scrollHeight + this.offset == 0) return;
		//this.TextArea.style.height = 'auto';
		//this.TextArea.style.height = (this.TextArea.scrollHeight + this.offset) + 'px';
		//this.OY = (this.TextArea.scrollHeight + this.offset);
	},

	_addInnerElement: function()
	{
		this.InnerElement = new SSVC.SVG.Group();
		this.InnerElement.Class = "LabelFrame";
		this.InnerRect = new SSVC.SVG.Rectangle();

		this.InnerRect.Class = "LabelRect";

		this.InnerTriangle = new SSVC.SVG.Triangle();
		this.InnerTriangle.Class = "LabelTriangle";

		this.InnerText = new SSVC.SVG.Text();
		this.InnerText.Class = "LabelText";
		this.InnerElement.addChild(this.InnerTriangle);
		this.InnerElement.addChild(this.InnerRect);



		//this.InnerElement.addChild(this.InnerText);



		this.TextArea = document.createElement("input");
		this.TextArea.type = "text";
		document.body.appendChild(this.TextArea);
		this.TextArea.className = "EditableTextArea";

		this.TextArea.id = "EditableTextArea_" + SSVC.SVG.Shape.MakeName(6);
		this.TextArea.setAttribute("rows", 1);
		this.TextArea.setAttribute("wrap", "soft");
		this.TextArea.style.display = "none";

		this.offset = !window.opera ? (this.TextArea.offsetHeight - this.TextArea.clientHeight) : (this.TextArea.offsetHeight + parseInt(window.getComputedStyle(this.TextArea, null).getPropertyValue('border-top-width')));

		this.TextArea.addEventListener('keypress', SSVC.Wrap.Dojo.Partial(this.OnTextAreaKeyPress, this));
		this.TextArea.addEventListener('keyup', SSVC.Wrap.Dojo.Partial(this.OnTextAreaKeyUp, this));
		SSVC.Wrap.Dojo.On(this.TextArea, 'focus', SSVC.Wrap.Dojo.Partial(this.OnTextAreaFocus, this));
		SSVC.Wrap.Dojo.On(this.TextArea, 'blur', SSVC.Wrap.Dojo.Partial(this.OnTextAreaBlur, this));
		SSVC.Wrap.Dojo.On(this.TextArea, 'click', SSVC.Wrap.Dojo.Partial(this.OnTextAreaMouseDown, this));
		SSVC.Wrap.Dojo.On(this.TextArea, 'dblclick', SSVC.Wrap.Dojo.Partial(this.OnTextAreaMouseDblClick, this));

	},

	_addSelection: function()
	{
		this._addSelectionTops();
	},

	_arrangeInnerElements: function(withoutText)
	{
		this.Radius = this.OY / 2;

		if (!withoutText)
			this._updateTextControl();
		//this.Frame.update(this.X, this.Y, this.OX, this.OY);
		//this.InnerElement.update(this.X, this.Y, this.OX, this.OY);
		this.InnerRect.setRadius(this.Radius);
		this.InnerRect.update(this.X, this.Y, this.OX, this.OY);
		this.InnerTriangle.update(

			{
				x: this.X + this.OX / 2 + this.OY / 2-3,
				y: this.Y + this.OY / 2
			}, {
				x: this.startX,
				y: this.startY
			}, {
				x: this.X + this.OX / 2 - this.OY / 2+3,
				y: this.Y + this.OY / 2
			});

		
	},

	_updateTextControl: function()
	{
		var rect = this.InnerRect.Node.getBoundingClientRect();
		this.TextArea.style.top = +rect.top + "px";
		this.TextArea.style.left = +rect.left + "px";
		this.TextArea.style.color = this.LineColor;

		this.TextArea.style.fontSize = this.FontSize + "pt";
		this.TextArea.style.lineHeight = this.FontSize + "px";
		this.TextArea.height = this.OY + "px";
		this.TextArea.style.borderRadius = this.Radius + "px";
		this.TextArea.style.borderSpacing = this.Radius+"px;"

		this.TextArea.style.fontFamily = this.FontFamily;

		//this.TextArea.style.marginLeft = this.Radius + "px";
		//this.TextArea.style.marginRight = this.Radius + "px";
		this.TextArea.style.width = ((this.TextArea.value.length + 1) * this.FontSize) + 'px';
		//this.TextArea.style.width = this.OX - this.Radius+"px";

		this._resizeTextHeight();
	},


	_arrangeGroupNode: function()
	{
		this.appendChild(this.InnerElement.Node);
		for (var i in this.Tops)
			this.appendChild(this.Tops[i].Node);
	},

	_addSelectionTops: function()
	{
		var top = new SSVC.SVG.Circle();
		top.Class = "LabelTop";
		top.Name = "BottomMiddle";
		top.Number = 1;

		SSVC.Wrap.Dojo.On(top.Node, 'mousedown', SSVC.Wrap.Dojo.Partial(this.OnTopMouseDown, this, top.Number));

		this.Tops.push(top);

		this._setCursorAttrForTops();
	},

	_setCursorAttrForTops: function()
	{
		this.Tops[0].Node.setAttribute('cursor', 'ne-resize');
	},

	_arrangeTops: function()
	{
		//todo: temporary cap
		var topRadius = SSVC.SVG.Shape.GetProperty("top");
		topRadius = 1;

		this.Tops[0].update(this.startX, this.startY, topRadius);
	},


	_refreshSelection: function()
	{
		switch (this.SelectMode)
		{
			case SSVC.SVG.ActiveElement.Enums.SelectMode.Contoured:
				this._showContour();
				this._showInnerElement();
				this._hideSelection();
				break;
			case SSVC.SVG.ActiveElement.Enums.SelectMode.Selected:
				this._showSelection();
				this._showInnerElement();
				this._hideContour();
				break;
			case SSVC.SVG.ActiveElement.Enums.SelectMode.None:
				this._hideContour();
				this._hideSelection();
				this._showInnerElement();
				break;
		}

	},

	_setTextAreaTop: function()
	{
		this.TextArea.style.zIndex = "100500";
	},
	_setTextAreaBack: function()
	{
		//this.TextArea.style.zIndex = "100";
	},

	_showContour: function()
	{
	},

	_hideContour: function()
	{
	},

	_showSelection: function()
	{
		this._showTops();
	},

	_hideSelection: function()
	{
		this._hideTops();
	},

	_showInnerElement: function()
	{
		this.TextArea.style.display = "block";
	},

	_hideInnerElement: function()
	{
		this.TextArea.style.display = "none";
	},

	_disableSelection: function(el)
	{
		el.addEventListener("selectstart", this._disabler);
	},

	_disabler: function(e)
	{
		if (e.preventDefault)
		{
			e.preventDefault();
		}
		return false;
	},

	_enableSelection: function(el)
	{
		el.removeEventListener("selectstart", this._disabler);
	},

	GetOuterXml: function()
	{
		/* We changed body of this function by removing <textarea> tag since it caused error
		"Object doesn't support property or method 'measureText' " */

		var properties = [];

		var attrXml = "";
		for (var i = 0; i < this.Node.attributes.length; i++)
		{
			attrXml += "{0}='{1}' ".Format(this.Node.attributes[i].name, this.Node.attributes[i].value);
		}

		for (var i = 0; i < this.TextArea.attributes.length; i++)
		{
			if (this.TextArea.attributes[i].name != "id")
				attrXml += "{0}='{1}' ".Format(this.TextArea.attributes[i].name, this.TextArea.attributes[i].value);
		}

		this.innerXml = "<{0} {1} text-height='{3}'>{2}</{0}>".Format("text", attrXml.trim(), this.TextArea.textContent, this.TextArea.style.height);

		var resultxml = SSVC.SVG.ShapePrototype.ParseXml(this.innerXml);

		return resultxml;

		//var properties = [];

		//var attrXml = "";
		//for (var i = 0; i < this.Node.attributes.length; i++)
		//{
		//	attrXml += "{0}='{1}' ".Format(this.Node.attributes[i].name, this.Node.attributes[i].value);
		//}
		//this.innerXml = "<{0} {1} text-height='{3}'>{2}</{0}>".Format("text", attrXml.trim(), this.TextArea.textContent, this.TextArea.style.height);

		//var textareaattrs = "";
		//for (var i = 0; i < this.TextArea.attributes.length; i++)
		//{
		//	textareaattrs += "{0}='{1}' ".Format(this.TextArea.attributes[i].name, this.TextArea.attributes[i].value);
		//}
		//var textareaxml = "<{0} {1}>{2}</{0}>".Format("textarea", textareaattrs.trim(), this.TextArea.textContent);

		//var resultxml = SSVC.SVG.ShapePrototype.ParseXml(this.innerXml);
		//var textarea = SSVC.SVG.ShapePrototype.ParseXml(textareaxml);
		//resultxml.appendChild(textarea);

		//return resultxml;
	},

	SetXml: function(xmldoc)
	{
		this.applyXml(xmldoc);

		this.TextArea.style.fontSize = this.OY * 0.85 + "px";
		this.TextArea.style.lineHeight = this.OY + "px";
		this.TextArea.style.color = this.LineColor;
		this.TextArea.style.height = xmldoc.getAttribute("text-height");

		this.TextArea.textContent = xmldoc.text;
		this.Refresh();
	},

	AddProperties: function(properties)
	{
		this.FontSize = (properties.fontSize) ? properties.fontSize : 14;
	},

	Fill: function(rgb)
	{
		this.TextArea.style.backgraund = rgb;
	}
}));
